package com.cskaoyan.service.front.collect;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.front.collect.CollectVo;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @author: jzc
 * @since: 2022/10/14 11:32
 **/
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData getCollectList(Byte type, Integer page, Integer limit, MarketUser marketUser) {
        ArrayList<CollectVo> collectVos = new ArrayList<>();
        CommonData<CollectVo> collectVoCommonData = new CommonData<>();
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        PageHelper.startPage(page,limit);
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andUserIdEqualTo(marketUser.getId());
        criteria.andTypeEqualTo(type);
        criteria.andDeletedEqualTo(false);
        List<MarketCollect> marketCollects =
                marketCollectMapper.selectByExample(marketCollectExample);
        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(marketCollects);
        for (MarketCollect marketCollect : marketCollects) {
            Integer valueId = marketCollect.getValueId();
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(valueId);
            CollectVo collectVo = new CollectVo(
                    marketGoods.getBrief(),
                    marketGoods.getPicUrl(),
                    marketCollect.getValueId(),
                    marketGoods.getName(),
                    marketCollect.getId(),
                    marketCollect.getType(),
                    marketGoods.getRetailPrice()
            );
            collectVos.add(collectVo);
            // marketCollect.get
        }
        collectVoCommonData.setPage(marketCollectPageInfo.getPageNum());
        collectVoCommonData.setLimit(limit);
        collectVoCommonData.setTotal((int) marketCollectPageInfo.getTotal());
        collectVoCommonData.setPages(marketCollectPageInfo.getPages());
        collectVoCommonData.setList(collectVos);
        return collectVoCommonData;
    }

    @Override
    public int addordelete(MarketUser primaryPrincipal, Byte type, Integer valueId) {
        Date date = new Date();
        Integer userId = primaryPrincipal.getId();
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if (type==0){
            criteria.andValueIdEqualTo(valueId);
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        //商品id找商品表的信息
        if (marketCollects.size()==0) {
            MarketCollect marketCollect = new MarketCollect(
                    null,
                    userId,
                    valueId,
                    type,
                    date,
                    date,
                    false
            );
            int effected = marketCollectMapper.insertSelective(marketCollect);
            return effected;

        }else {
            MarketCollect marketCollect = new MarketCollect(
                    null,
                    userId,
                    valueId,
                    type,
                    date,
                    date,
                    true
            );
            int effected = marketCollectMapper.updateByPrimaryKeySelective(marketCollect);
            return effected;
        }
    }

}
